package com.waypolice.manager.controller;

import com.waypolice.manager.service.*;
import com.waypolice.common.utils.PageUtils;
import com.waypolice.pojo.UpmsOrganization;
import com.waypolice.pojo.UpmsUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.binary.Base64;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.http.*;
import java.io.IOException;
import java.util.*;

/**
 * 个人信息Controller层
 * 
 * @author wr
 * 
 */
@Controller
@Api(value = "个人信息Controller层", description = "个人信息Controller层")
public class UserAction {
	private static final Logger logger = LoggerFactory.getLogger(UserAction.class);

	@Autowired
	private UserService userService;

	@Autowired
	private UpmsOrganizationService upmsOrganizationService;

	/**
	 * 个人信息
	 * @param response
	 * @param request
	 * @return
	 */
	@ApiOperation(value = "个人信息跳转")
	@RequestMapping(value="/pcwindow/userDetail")
	public String mianJump(HttpServletResponse response,
						   HttpServletRequest request) {
		return "/userManager/userDetail";
	}

	@ApiOperation(value = "修改密码页面跳转")
	@RequestMapping(value = "/editPassword", method = RequestMethod.GET)
	public String editPassword(HttpServletResponse response,
							 HttpServletRequest request) {
		return "/userManager/editPassword";
	}

	/**
	 * 用户管理页面跳转
	 * @param map
	 * @param response
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/pcwindow/userManager")
	public String userManagerJump(ModelMap map, HttpServletResponse response,
						   HttpServletRequest request) {
		return "/userManager/user";
	}

	@ApiOperation(value = "查询所有用户")
	@RequestMapping(value = "/pcwindow/findAllUser",method = RequestMethod.GET)
	@ResponseBody
	public Object findAllUser(@RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
							  @RequestParam(required = false, defaultValue = "10", value = "limit") int limit) {
		Map<String, Object> result = new HashMap<>(2);
		List<Map<String, Object>> userList=userService.findAllUser();
		if(!userList.isEmpty()){
			//最终的list集合,分页操作
			List<Map<String, Object>> finalList = new ArrayList<Map<String, Object>>();
			PageUtils<Map<String, Object>> pageUtils = new PageUtils<Map<String, Object>>();
			finalList = pageUtils.pageList(userList,offset,limit);
			result.put("rows",finalList);
			result.put("total",userList.size());
		}else{
			logger.info("查询用户列表失败，请稍后在试......");
			result = null;
		}
		return result;
	}

	/**
	 * 条件查询所有用户
	 * @return
	 */
	@ApiOperation(value = "条件查询所有用户")
	@RequestMapping(value = "/pcwindow/findUserByTerm")
	@ResponseBody
	public Object findUserByTerm(@RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
								 @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
								 HttpServletRequest request,HttpServletResponse response) {
		String userName = request.getParameter("userName");
		String cityName = request.getParameter("cityName");
		List userList = null;
		Map<String, Object> result = new HashMap<>(2);
		//最终的list集合,分页操作
		List<Map<String, Object>> finalList = new ArrayList<Map<String, Object>>();
		PageUtils<Map<String, Object>> pageUtils = new PageUtils<Map<String, Object>>();
		finalList = pageUtils.pageList(userList,offset,limit);
		result.put("rows",finalList);
		result.put("total",userList.size());
		return result;
	}
	/**
	 * 根据Id查询用户
	 * @return
	 */
	@ApiOperation(value = "条件查询所有用户")
	@RequestMapping(value = "/pcwindow/findUserByUserId")
	@ResponseBody
	public List<Map<String, Object>> findUserByUserId(HttpServletRequest request,HttpServletResponse response) {
		String userId = request.getParameter("userId");
		List<Map<String, Object>> userList = null;
		return userList;
	}
	/**
	 * 根据Id查询用户
	 * @return
	 */
	@ApiOperation(value = "条件查询所有用户")
	@RequestMapping(value = "/pcwindow/getLoginUser")
	@ResponseBody
	public UpmsUser getLoginUser(HttpServletRequest request,HttpServletResponse response) {
		Subject subject = SecurityUtils.getSubject();
		UpmsUser user = (UpmsUser) subject.getPrincipal();
		return user;
	}

	/**
	 * @author:zyp
	 * @description 根据用户获取起组织内容
	 * @date: 2019/3/18 9:47
	 * @return 
	 */
	@ApiOperation(value = "登录人组织信息")
	@RequestMapping(value = "/pcwindow/getOrganization")
	@ResponseBody
	public Object getOrganization(){
		//该用户无组织无纪律
		String flag="0";
		List<UpmsOrganization> organization=new ArrayList<>();
		//获取当前登录人的用户id（账号）
		Subject subject = SecurityUtils.getSubject();
		String username = (String) subject.getPrincipal();
		//后续获取用户对象
		List<UpmsUser> users = userService.findByName(username);
		UpmsUser user = new UpmsUser();
		if(users!=null&&users.size()>0){
			user = users.get(0);
		}
		organization = upmsOrganizationService.getOrganization(user.getUserId());
		if(null==organization||organization.size()==0){
			return flag;
		}
		return organization;
	}

	public static String Base64Decoding(String code) {
        String charset = "utf-8";
        byte[] reqdataBytes;
        try {
            reqdataBytes = code.getBytes(charset);
            String reqdata = new String(Base64.decodeBase64(reqdataBytes), "UTF-8");
            return reqdata;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}